// pages/order/order.js
let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderData: null, //提交订单的商品数据
        num: 0, //合计商品数量
        totalPrice: 0, //合计总价
        orderTime: 0, //订单时间
        addressList: null, //地址列表
        defaultAddress: null, //默认地址
        show: false, //地址弹框
        sidList: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    // 获取结算页面数据
    onLoad(options) {
        let sid = options.sid
        this.setData({
            sidList: sid
        })
        wx.request({
            url: 'http://www.kangliuyong.com:10002/commitShopcart',
            data: {
                appkey: app.globalData.appkey,
                tokenString: app.globalData.token,
                sids: sid
            },
            success: res => {
                console.log(res);
                this.getTime(res.data.result)
            }
        })
    },
    // 处理商品数据
    getTime(item) {
        console.log(item);
        let time = item[item.length - 1].updated_at.split("T")
        let yearMonthDay = time[0]
        let str = time[1].split(".")
        let minSecond = str[0]
        let orderTime = yearMonthDay + " " + minSecond
        console.log(orderTime);
        //获取商品数量
        let num = this.data.num
        for (let i = 0; i < item.length; i++) {
            num += item[i].count
        }
        this.setData({
            orderData: item,
            num: num,
            orderTime: orderTime
        })
        this.getTotalPrice()
        console.log(this.data.orderData);
    },
    // 查找地址
    onShow() {
        wx.request({
            url: 'http://www.kangliuyong.com:10002/findAddress',
            data: {
                appkey: app.globalData.appkey,
                tokenString: app.globalData.token
            },
            success: res => {
                console.log(res);
                this.getSort(res.data.result)
            }
        })
    },
    // 处理地址数据 排序
    getSort(data) {
        data.sort(function (a, b) {
            let x = a.isDefault
            let y = b.isDefault
            if (x < y) {
                return 1;
            }
            if (x > y) {
                return -1;
            }
            return 0;
        });
        console.log(data);
        for (let i = 0; i < data.length; i++) {
            // 处理数据添加一个判断是否选中的值
            if (data[i].isDefault == 1) {
                data[i].checked = true;
            }
            if (i == 0) {
                data[i].checked = true;
            } else {
                data[i].checked = false;
            }
        }
        this.setData({
            addressList: data,
            defaultAddress: data[0]
        })
    },
    // onClickLeft() {
    //     wx.showToast({
    //         title: '点击返回',
    //         icon: 'none'
    //     });
    // },
    //计算总价
    getTotalPrice() {
        let orderData = this.data.orderData
        let total = 0
        for (let i = 0; i < orderData.length; i++) {
            total += orderData[i].price * orderData[i].count
        }
        this.setData({
            orderData: orderData,
            totalPrice: total.toFixed(2)
        })
    },
    // 立即结算
    settlement() {
        wx.request({
            url: 'http://www.kangliuyong.com:10002/pay',
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
            },
            data: {
                appkey: app.globalData.appkey,
                tokenString: app.globalData.token,
                sids: this.data.sidList,
                phone: this.data.defaultAddress.tel,
                address: this.data.defaultAddress.addressDetail,
                receiver: this.data.defaultAddress.name
            },
            success: (res) => {
                console.log(res);
                if (res.data.msg == '结算成功') {
                    // 结算成功清除选中的sid
                    wx.removeStorageSync('select')
                    wx.showToast({
                        title: res.data.msg,
                    })
                    wx.navigateTo({
                      url: '/pages/myorder/myorder',
                    })
                } else {
                    wx.showToast({
                        title: '结算失败',
                        icon: 'error'
                    })
                }
            }
        })
    },
    // 显示地址弹框
    showPopup() {
        this.setData({
            show: true
        });
    },
    // 隐藏地址弹框
    onClose() {
        this.setData({
            show: false
        });
    },
    // 复选框
    onChange(event) {
        let aid = event.currentTarget.dataset.aid
        console.log(aid);
        let addressList = this.data.addressList
        let obj = null
        for (let i = 0; i < addressList.length; i++) {
            addressList[i].checked = false
            console.log(addressList[i]);
            console.log(addressList[i].aid);
            if (addressList[i].aid == aid) {
                addressList[i].checked = !addressList[i].checked
                obj = addressList[i]
            }
        }
        this.setData({
            addressList: addressList,
            defaultAddress: obj
        })
    },
    // 新增地址
    newAdddress() {
        wx.navigateTo({
            url: '/pages/new-address/new-address',
        })
    }
})